package app.project.directionlensproject;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.widget.TextView;
import app.project.directionlensproject.fragment.FgListPlace;
import app.project.directionlensproject.util.BundleKeys;
import app.project.directionlensproject.util.Utilities;

import com.google.android.gms.maps.model.LatLng;
import com.viewpagerindicator.TabPageIndicator;

public class SearchActivity extends GeneralActivity {
	private String searchStr;
	private TextView tvName;
	double locationX, locationY;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_search);
		tvName = (TextView) findViewById(R.id.tv_name);
		searchStr = getIntent().getStringExtra(BundleKeys.SEARCH_STR);
		initSearch(searchStr);
	}

	private void initSearch(String searchStr) {
		tvName.setText(getString(R.string.search) + ": " + searchStr);
		LatLng myPlace = Utilities.getInstance().getMyPlace(this);
		locationX = myPlace.latitude;
		locationY = myPlace.longitude;

		SearchTapAdapter adapter = new SearchTapAdapter(getSupportFragmentManager());

		ViewPager pager = (ViewPager)findViewById(R.id.pager);
		pager.setAdapter(adapter);
		TabPageIndicator indicator = (TabPageIndicator)findViewById(R.id.indicator);
		indicator.setViewPager(pager);
	}

	private class SearchTapAdapter extends FragmentStatePagerAdapter {
		public SearchTapAdapter(FragmentManager fm) {
			super(fm);
		}

		@Override
		public Fragment getItem(int position) {
			Bundle b = new Bundle();
			b.putDouble(BundleKeys.LOCATION_X, locationX);
			b.putDouble(BundleKeys.LOCATION_Y, locationY);
			b.putString(BundleKeys.SEARCH_STR, searchStr);
			if (position != 0) {
				b.putBoolean(BundleKeys.IS_GOOGLE, true);
				b.putInt(BundleKeys.TYPE_PLACE, FgListPlace.TYPE_GOOGLE_SEARCH);
			} 
			FgListPlace f = new FgListPlace();
			f.setArguments(b);
			return f;
		}

		@Override
		public CharSequence getPageTitle(int position) {
			if (position == 0) {
				return getApplicationContext().getString(R.string.hom_tab1);
			} else
				return getApplicationContext().getString(R.string.hom_tab2);
		}

		@Override
		public int getCount() {
			return 2;
		}
	}
}
